logo

The best IT Trainig Institute In Gurgaon

Perform Mouse Hover

Mouse hover actions in Selenium involve moving the mouse pointer over a web element without clicking it. This action is commonly used to reveal hidden elements, like dropdown menus, tooltips, or other interactive elements that appear only on mouseover.

How to Perform Mouse Hover Actions in Selenium:

To perform a mouse hover action, you typically use the Actions class in Selenium WebDriver, which provides advanced user interactions like hovering, clicking, dragging, and dropping.

    Basic
  • Initialize the WebDriver and Open the Page:
    Set up the WebDriver and navigate to the desired web page.
  • Locate the Web Element:
    Identify the web element over which you want to hover the mouse.
  • Use the Actions Class:
    Create an instance of the Actions class and use it to perform the hover action.
  • Perform the Action:
    Use the move To Element() method to move the mouse to the specified element, and then call build() .perform() to execute the action.
package asc;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import io.github.bonigarcia.wdm.WebDriverManager;

public class MouseHover {

public static void main(String[] args) {
		
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.ebay.com/");
driver.manage().window().maximize();

WebElement element = driver.findElement(By.xpath("//*[@id=\"vl-flyout-nav\"]/ul/li[9]/a"));
System.out.println(element);
Actions action = new Actions(driver);
action.moveToElement(element).perform();
}

}
                
code
Output
output
Code Breakdown and Explanation:

1. Setup and Initialization:

                    
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
                    
                
  • WebDriverManager.chromedriver().setup();: 
    This sets up the ChromeDriver using WebDriverManager, ensuring the correct version of the ChromeDriver is used.
  • new ChromeDriver();:
    Initializes a new instance of the ChromeDriver, which is the WebDriver implementation for Google Chrome.

Opening the Website:

                    
driver.get("https://www.ebay.com/");
driver.manage().window().maximize();
                    
                
  • driver.get("https://www.ebay.com/");:
    Navigates the browser to the eBay homepage.
  • driver.manage().window().maximize();:
    Maximizes the browser window for better visibility and interaction.

Locating the Web Element:

                    
WebElement element = driver.findElement(By.xpath("//*[@id=\"vl-flyout-nav\"]/ul/li[9]/a"));
                    
                
  • river.findElement(By.xpath(...));:
    Finds the specific web element on the page using an XPath expression. This XPath points to an anchor tag within a list item in a menu. In the context of eBay's website, this might correspond to a category in their navigation menu, such as "Motors," "Fashion," etc.

Printing the WebElement (Optional):

                    
System.out.println(element);
                    
                
  • System.out.println(element);:
    Prints the WebElement to the console. This output typically includes a string representation of the element's details, useful for debugging.

Performing the Mouse Hover Action:

                    
Actions action = new Actions(driver);
action.moveToElement(element).perform();
                    
                
  • new Actions(driver);:
    Creates an instance of the Actions class, which is used to perform advanced user interactions like mouse movements, clicks, drag-and-drop, etc.
  • action.moveToElement(element).perform();:
    Moves the mouse to the center of the specified element (element) and performs the hover action. This action usually triggers a change in the UI, such as displaying a dropdown menu or revealing additional options.
Key Points
    WebDriverManager :
  • Simplifies the management of browser drivers, ensuring compatibility with the browser version in use.
  • Actions Class :
  • Essential for performing complex user interactions beyond simple clicks, such as hovering, double-clicking, or right-clicking.
  • XPath Locator:
  • Used here to precisely locate the desired web element based on its position in the DOM. XPath is powerful but can be sensitive to changes in the web page structure.
  • Maximizing the Browser Window: 
  • This is a good practice in test automation to ensure all elements are visible and to avoid issues related to viewport size.
Considerations:
Element Identification:
  • Ensure the XPath used is correct and stable, as any changes in the web page structure can cause the locator to fail.
  • Browser Compatibility:
  • The behavior of hover actions can vary slightly across different browsers. It's important to test across multiple browsers if the application is expected to work on them.
  • Waits and Synchronization:
  • In some cases, additional waits may be necessary to ensure that the page or specific elements are fully loaded before performing actions. Explicit waits (like WebDriverWait) are more reliable than implicit waits or thread sleeps.
  • Mouse hover actions are particularly useful for interacting with dynamic content that appears only on mouseover, such as dropdown menus, tooltips, or interactive elements in navigation bars.